


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import watij.elements.*;
import watij.runtime.ie.*;

/**
 * Servlet implementation class ReturnNoOfElements_XML
 */
public class ReturnNoOfElements_XML extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReturnNoOfElements_XML() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter pw = response.getWriter();
		String url = request.getParameter("url");
		String name = request.getParameter("name");
		String type = request.getParameter("type");
		try
		{
		IE ie = new IE();
		ie.start(url);
		String XMLResponse = "<?xml version=\"1.0\"?>";
		if(type.equals("Radio"))
		{
			Radios radio = ie.radios();
			XMLResponse += "<Radios language=\"en_US\">";
			for(int i=0; i<radio.length(); i++)
			{
				if(radio.radio(i).name().equals(name))
				{
					XMLResponse += "<Button>";
					XMLResponse += "<ButtonName>" + radio.radio(i).value() +"</ButtonName>";
					XMLResponse += "<ButtonGroup>" + radio.radio(i).name() +"</ButtonGroup>";
					XMLResponse += "</Button>";
				}
			}
			XMLResponse += "</Radios>";
			response.setContentType("text/xml");
			response.setHeader("Cache-Control", "no-cache");
			response.getWriter().write(XMLResponse);
			ie.close();
		}
		
		else if(type.equals("CheckBox"))
		{
			Checkboxes checkbox = ie.checkboxes(); 
			XMLResponse += "<CheckBoxes language=\"en_US\">";
			for(int i=0; i<checkbox.length(); i++)
			{
				if(checkbox.checkbox(i).name().equals(name))
				{
					XMLResponse += "<Checkbox>";
					XMLResponse += "<CheckBoxName>" + checkbox.checkbox(i).value() +"</CheckBoxName>";
					XMLResponse += "<CheckBoxGroup>" + checkbox.checkbox(i).name() + "</CheckBoxGroup>";
					XMLResponse += "</Checkbox>";
				}
			}
			XMLResponse += "</CheckBoxes>";
			response.setContentType("text/xml");
			response.setHeader("Cache-Control", "no-cache");
			response.getWriter().write(XMLResponse);
			ie.close();
		}
		
		else if(type.equals("SelectList"))
		{
			SelectLists list = ie.selectLists();
			XMLResponse += "<SelectList language=\"en_US\">";
			for(int i=0; i<list.length(); i++)
			{
				if(list.selectList(i).name().equals(name))
				{
					for(int j=0;j<list.selectList(i).options().length();j++)
					{
						XMLResponse += "<List>";
						XMLResponse += "<ListItem>" + list.selectList(i).option(j).value() +"</ListItem>";
						XMLResponse += "<ListName>" + list.selectList(i).name()+"</ListName>";
						XMLResponse += "</List>";
					}
				}
			}
			XMLResponse += "</SelectList>";
			response.setContentType("text/xml");
			response.setHeader("Cache-Control", "no-cache");
			response.getWriter().write(XMLResponse);
			ie.close();
		}
		
		else if(type.equals("Hidden"))
		{
			Hiddens hidden = ie.hiddens();
			XMLResponse += "<Hidden language=\"en_US\">";
			for(int i=0; i<hidden.length(); i++)
			{
				if(hidden.hidden(i).name().equals(name))						
					XMLResponse += "<Item>" + hidden.hidden(i).value() +"</Item>";					
			}
			XMLResponse += "</Hidden>";
			response.setContentType("text/xml");
			response.setHeader("Cache-Control", "no-cache");
			response.getWriter().write(XMLResponse);
			ie.close();
		}
		}
		catch(Exception ex)
		{
			pw.println(ex.getMessage());
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
